﻿var map;
var geocoder;
// set the default loaction
var latlong = new GLatLng(53.5, -2);
var level = 5;

function initMap() {
    if (!GBrowserIsCompatible()) { return; }

    map = new GMap2(document.getElementById("map_canvas_events"));
    map.enableScrollWheelZoom();
    map.addControl(new GSmallMapControl());
    map.setCenter(latlong, level);
    geocoder = new GClientGeocoder();
    map.openInfoWindow(map.getCenter(), "<b>Input address and drag it...</b>");

}

// current removed method
function clicked(overlay, latlng) {
    if (latlng) {
        geocoder.getLocations(latlng, function(addresses) {
            if (addresses.Status.code != 200) {
                alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
            }
            else {
                // addAddressToMap(response);
            }
        });
    }
}



// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point, { draggable: true });
        map.addOverlay(marker);
        map.setCenter(point, 15);
        marker.openInfoWindowHtml(place.address + '<br /><p><b>You can drag and drop the point to locate your address.</b></p>');


        // give value to Latitude and Longitude
        if (document.getElementById("Latitude") != null || document.getElementById("Longitude") != null) {
            document.getElementById("Latitude").value = point.lat();
            document.getElementById("Longitude").value = point.lng();
        }

        // dragstart
        GEvent.addListener(marker, "dragstart", function() {
            map.closeInfoWindow();
        });


        GEvent.addListener(marker, "dragend", function() {
            point = marker.getPoint();
            geocoder.getLocations(point, function(addresses) {
                if (addresses.Status.code != 200) {
                    document.getElementById("address-textbox").value = "unknown location"
                    alert("failed to find an address for " + point.toUrlValue());
                }
                else {
                    address = addresses.Placemark[0];
                    var myHtml = '<b>Address:</b>&nbsp;' + address.address + '<br/>' +
		                '<b>Updated Coordinates:</b>&nbsp;' + point.toUrlValue();

                    document.getElementById("address-textbox").value = address.address;

                    // give value to Latitude and Longitude
                    if (document.getElementById("Latitude") != null || document.getElementById("Longitude") != null) {
                        document.getElementById("Latitude").value = point.lat();
                        document.getElementById("Longitude").value = point.lng();
                    }

                    marker.openInfoWindow(myHtml);
                }
            });
        });
    }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
    var address = document.getElementById("address-textbox").value;
    geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
    document.getElementById("address-textbox").value = address;
    showLocation();
}


